package 第二周;

/*
 * @author  YxinMiracle
 * @date  2022-04-25 17:05
 * @Gitee: https://gitee.com/yxinmiracle
 */

import java.util.Arrays;

public class 统计优美子数组 {

    public int numberOfSubarrays(int[] nums, int k) {
        int result = 0;
        int[] sumArray = new int[nums.length + 1];
        sumArray[0] = 0;
        int[] count = new int[nums.length+1];
        count[sumArray[0]]++;
        for (int i = 1; i <= nums.length; i++) {
            sumArray[i] = sumArray[i - 1] + nums[i - 1] % 2;
            if (sumArray[i]>=k){
                result += count[sumArray[i]-k];
            }
            count[sumArray[i]]++;
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,1,2,1,1};
        int k = 3;
        System.out.println(new 统计优美子数组().numberOfSubarrays(nums,k));
    }


}
